/*
 * GUI : Display Jtree, princioal frame
 * @authors Andres F. Restrepo A.
 *
 * @version 0.1
 * Date : Jul 24, 2011, 11:56 PM
*/

package com.andres.gui;

import com.andres.webdav.*;
import com.googlecode.sardine.DavResource;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;


public class Explorer extends JFrame implements ActionListener, Runnable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 994392040357787909L;
	private JTree tree;
	private JToolBar toolBar;
	private JButton btnRefresh;
	private JButton btnSelection;
	private JButton btnAdminServer;
	private JProgressBar pgBar;
	private JScrollPane scrollPane;
	private Connection sardineInstance;
	private int contBar;
	public Explorer(){
		
		toolBar = new JToolBar();
		btnRefresh = new JButton("Refresh");
		btnSelection = new JButton("choice");
		btnAdminServer = new JButton("Servers");
		btnRefresh.addActionListener(this);
		btnSelection.addActionListener(this);
		btnAdminServer.addActionListener(this);
		pgBar = new JProgressBar();
		toolBar.add(btnRefresh);
		toolBar.add(btnSelection);
		toolBar.add(btnAdminServer);
		toolBar.add(pgBar);
		
		
		//add
		getContentPane().add(toolBar,BorderLayout.NORTH);
		scrollPane = new JScrollPane();
		getContentPane().add(scrollPane);
		setTitle("Webdav explorer");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(340, 600);
		setVisible(true);
		
	}
	/*
	private void initTree(){
		
		if(tree != null){
			scrollPane.remove(tree);
		}
		DefaultMutableTreeNode webdavJtree = new DefaultMutableTreeNode("Webdav");
		DefaultTreeModel modelo = new DefaultTreeModel(webdavJtree);
	    tree = new JTree(modelo);
	    ((DefaultTreeModel)tree.getModel()).nodeChanged(webdavJtree);
		DefaultMutableTreeNode n = new DefaultMutableTreeNode(sardineInstance.getUrlBase());
		modelo.insertNodeInto(n, webdavJtree, 0);
		System.out.println(sardineInstance.getPath());
		setEnabled(false);
		try{
		buildTree(sardineInstance.getPath(), n);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		setEnabled(true);
	//	txtMensaje.setText("OK...");
		scrollPane.setViewportView(tree);
		System.out.println(modelo.getChild(webdavJtree,0));
		
		
	}
	*/
	private void initWebdav(){
		
		ServerAdmin sa =new ServerAdmin();
		if (!ServerAdmin.cancel){
			String hostName = sa.getHostname();
			String user = sa.getUser();
			String password = sa.getPassword();
			String path = sa.getPath();
			if((hostName.equals(""))||(path.equals(""))){
				JOptionPane.showMessageDialog(this, "Not selecting Directory");
			}else{
				sardineInstance = new Connection(hostName,path,user,password);
				//initTree();
				setJProgressbar();
				Thread hilo = new Thread(this);
				hilo.start();
			}
		}
		
	}
	private void setJProgressbar(){
		List<DavResource> lista = sardineInstance.getListResource(sardineInstance.getUrlBase()+sardineInstance.getPath());
		int max = lista.size();
		pgBar.setMinimum(0);
		pgBar.setMaximum(max);
	}

	private void buildTree(String path,DefaultMutableTreeNode rootNode){
		if (path.contains(" ")) {
			path = path.replace(" ", "%20");
		}
		List<DavResource> lista = sardineInstance.getListResource(sardineInstance.getUrlBase()+path);
		System.out.println(lista);
		if(lista != null){
			
		for (int i = 1; i < lista.size(); i++) {
			if (lista.get(i).isDirectory()) {
				DefaultMutableTreeNode nodeDirectory = new DefaultMutableTreeNode(lista.get(i).getName());
				
				buildTree(lista.get(i).getPath(), nodeDirectory);
				rootNode.add(nodeDirectory);
				
			}else{
				DefaultMutableTreeNode node = new DefaultMutableTreeNode(lista.get(i).getName());
				rootNode.add(node);
				
				
			}
		} 
		pgBar.setValue(contBar++);
		pgBar.setStringPainted(true);
		}
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(btnRefresh)){
			if(tree!=null){
				contBar=0;
				Thread hilo1 =new Thread(this);
				hilo1.start();
			}
			
		}else if(e.getSource().equals(btnSelection)){
			try{
	        TreePath path = tree.getSelectionPath(); 
	        if(path != null) 
	            System.out.println(path.toString()); 
			}catch(NullPointerException ex){
				System.out.println(ex.getMessage());
				JOptionPane.showMessageDialog(this, "Error, Nothing selected");
			}

		}else if(e.getSource().equals(btnAdminServer)){
			contBar=0;
  	    	initWebdav();
		}
		
	}
	@Override
	public void run() {
		if(tree != null){
			scrollPane.remove(tree);
		}
		DefaultMutableTreeNode webdavJtree = new DefaultMutableTreeNode("Webdav");
		DefaultTreeModel modelo = new DefaultTreeModel(webdavJtree);
	    tree = new JTree(modelo);
	    ((DefaultTreeModel)tree.getModel()).nodeChanged(webdavJtree);
		DefaultMutableTreeNode n = new DefaultMutableTreeNode(sardineInstance.getUrlBase());
		modelo.insertNodeInto(n, webdavJtree, 0);
		System.out.println(sardineInstance.getPath());
		setEnabled(false);
		try{
		buildTree(sardineInstance.getPath(), n);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		setEnabled(true);
	//	txtMensaje.setText("OK...");
		scrollPane.setViewportView(tree);
		System.out.println(modelo.getChild(webdavJtree,0));
	}
}
